package com.upcode.weighing.admin.controller.sys;

import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.common.util.StringUtils;
import com.upcode.weighing.domain.SysEmp;
import com.upcode.weighing.log.OpsLog;
import com.upcode.weighing.service.SysOrganizationService;
import com.upcode.weighing.util.SensitiveInfoUtils;
import com.upcode.weighing.vo.req.PageReqVo;
import com.upcode.weighing.vo.req.pc.client.TuoMinRrturnVo;
import com.upcode.weighing.vo.req.pc.client.TuoMinVo;
import com.upcode.weighing.vo.resp.org.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 组织机构管理 前端控制器
 * </p>
 *
 * @author zhangteng
 * @since 2021-01-14
 */
@RestController
@RequestMapping("/sysOrganization")
@Api(value = "/sysOrganization",description = "机构管理",produces = MediaType.ALL_VALUE)
public class SysOrganizationController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysOrganizationController.class);

    @Autowired
    private SysOrganizationService sysOrganizationService;

    @OpsLog(operation = "机构管理/组织用户分页列表/查询" ,type = 1)
    @ApiOperation(value = "组织用户分页列表",notes ="")
    @PostMapping("/selectPageList")
    public Object selectPageList(@RequestBody PageReqVo<OrgEmpPageListVo> vo){
        return success(sysOrganizationService.selectPageList(vo));
    }

    @OpsLog(operation = "机构管理/添加组织机构/新增" ,type = 2)
    @ApiOperation(value = "添加组织机构",notes ="")
    @PostMapping("/addOrganization")
    public Object addOrganization(@RequestBody OrgAddVo vo){
        //判断资源是否存在
        int count=sysOrganizationService.getOrganizationByCode(vo.getCode());
        if(count>0){
            return error("该编码已存在");
        }
        Long type=sysOrganizationService.checkType(vo.getPid());
        if(vo.getAdd()==0){//添加公司
            vo.setPid(0L);//父级ID为0
            vo.setType(1);
        }else if(vo.getAdd()==1){//添加部门
            if(type==1){
                vo.setType(2);
            }else{
                return error("请选中公司");
            }
        }else if(vo.getAdd()==2){//添加岗位
            if(type==2){
                vo.setType(3);
            }else{
                return error("请选中部门");
            }
        }
        int i=sysOrganizationService.addOrganization(vo);
        if(i>0){
            return success("添加成功");
        }
        return error("添加失败");
    }

    @OpsLog(operation = "机构管理/获取组织机构列表/查询" ,type = 1)
    @ApiOperation(value = "获取组织机构列表",notes ="")
    @PostMapping("/getOrganizationList")
    public Object getOrganizationList(){
        return success(sysOrganizationService.getOrganizationList());
    }

    @OpsLog(operation = "机构管理/添加员工/新增" ,type = 2)
    @ApiOperation(value = "添加员工",notes ="")
    @PostMapping("/addOrgEmp")
    public Object addOrgEmp(@RequestBody SysEmpAddVo vo){
        if(sysOrganizationService.checkEmp(vo.getUserCode())>0){
            return error("该员工编码已存在");
        }
        int i=sysOrganizationService.addEmp(vo);
        if(i>0){
            return success("添加成功");
        }
        return error("添加失败");
    }

    @OpsLog(operation = "机构管理/获取所有公司信息列表/查询" ,type = 1)
    @ApiOperation(value = "获取所有公司信息列表",notes ="")
    @GetMapping("/getOrgCompany")
    public Object getOrgCompany(){
        return success(sysOrganizationService.getOrgCompany());
    }

    @OpsLog(operation = "机构管理/根据公司ID获取部门列表/查询" ,type = 1)
    @ApiOperation(value = "根据公司ID获取部门列表",notes ="")
    @GetMapping("/getOrgDept")
    public Object getOrgDept(Long id){
        return success(sysOrganizationService.getOrgDept(id));
    }

    @OpsLog(operation = "机构管理/根据部门ID获取岗位列表-用户/查询" ,type = 1)
    @ApiOperation(value = "根据部门ID获取岗位列表-用户",notes ="")
    @GetMapping("/getOrgGw")
    public Object getOrgGw(Long id){
        return success(sysOrganizationService.getOrgGw(id));
    }

    @OpsLog(operation = "机构管理/批量删除组织机构/删除" ,type = 3)
    @ApiOperation(value = "批量删除组织机构",notes ="")
    @PostMapping("/upStatus")
    public Object upStatus(@RequestBody OrgDelVo vo){
        //解除组织与员工的绑定
        sysOrganizationService.delOrgEmp(vo);
        //解除组织与用户的绑定
        sysOrganizationService.delOrgUser(vo);
        int i=sysOrganizationService.delOrg(vo);
        if(i>0){
            return success("删除成功");
        }
        return error("删除失败");
    }

    @OpsLog(operation = "机构管理/修改删除组织机构/修改" ,type = 4)
    @ApiOperation(value = "修改删除组织机构",notes ="")
    @PostMapping("/upOrgName")
    public Object upOrgName(@RequestBody OrgEditVo vo){
        if(!StringUtils.isEmpty(vo.getName())){
            return error("修改的名称不能为空！");
        }
        int i=sysOrganizationService.upOrgName(vo);
        if(i>0){
            return success("修改成功");
        }
        return error("修改失败");
    }

    @OpsLog(operation = "机构管理/删除员工/删除" ,type = 3)
    @ApiOperation(value = "删除员工",notes ="")
    @PostMapping("/delEmp")
    public Object delEmp(@RequestBody SysEmpDelVo vo){
        //删除员工与用户的绑定
        List<String> list=sysOrganizationService.getEmpName(vo.getIds());
        if(list.size()>0){
            String[] str=list.toArray(new String[list.size()]);
            sysOrganizationService.delEmpAndUser(str);
        }
        int i=sysOrganizationService.delEmp(vo);
        if(i>0){
            return success("删除成功");
        }
        return error("删除失败");
    }

    @OpsLog(operation = "机构管理/修改员工/修改" ,type = 4)
    @ApiOperation(value = "修改员工",notes ="")
    @PostMapping("/upEmp")
    public Object upEmp(@RequestBody SysEmpEditVo vo){
        int i=sysOrganizationService.upEmp(vo);
        if(i>0){
            return success("修改成功");
        }
        return error("修改失败");
    }

    @OpsLog(operation = "机构管理/获取员工信息/查询" ,type = 1)
    @ApiOperation(value = "获取员工信息",notes ="")
    @PostMapping("/getEmpById")
    public Object getEmpById(@RequestBody SysEmpDelVo vo){
        SysEmp sysEmp=sysOrganizationService.getEmpById(vo);
        if(sysEmp!=null){
            sysEmp.setCard(SensitiveInfoUtils.sensitiveIdCard(sysEmp.getCard()));
            sysEmp.setMobile(SensitiveInfoUtils.sensitiveTel(sysEmp.getMobile()));
            sysEmp.setLinkMobile(SensitiveInfoUtils.sensitiveTel(sysEmp.getLinkMobile()));
            return success(sysEmp);
        }
        return error("获取失败");
    }
    @OpsLog(operation = "机构管理/脱敏/查询" ,type = 1)
    @PostMapping("/tuomin")
    @ApiOperation(value = "机构脱敏查询")
/*    @ApiImplicitParams(
            {       @ApiImplicitParam(value = "项目id" ,name = "id",required=true),
                    @ApiImplicitParam(value = "1身份证  2电话 3紧急联系人电话" ,name = "type",required=true),
            }
    )*/
    public Object tuomin(@RequestBody TuoMinVo vo){
        TuoMinRrturnVo tr = new TuoMinRrturnVo();
        SysEmpDelVo vo1= new SysEmpDelVo();
        vo1.setId(vo.getId());
        SysEmp sysEmp=sysOrganizationService.getEmpById(vo1);
        if (1==vo.getType()){
            tr.setValue2(SensitiveInfoUtils.sensitiveIdCard(sysEmp.getCard()));
            tr.setValue1(sysEmp.getCard());
        }
        if (2==vo.getType()){
            tr.setValue2(SensitiveInfoUtils.sensitiveIdCard(sysEmp.getMobile()));
            tr.setValue1(sysEmp.getMobile());
        }
        if (3==vo.getType()){
            tr.setValue2(SensitiveInfoUtils.sensitiveIdCard(sysEmp.getLinkMobile()));
            tr.setValue1(sysEmp.getLinkMobile());
        }
        return success(tr);
    }



}
